package logic

import (
	"context"
	"doctor-medicine-system/models/mysql"
	"gorm.io/gorm"

	"doctor-medicine-system/doctorrpc/doctorrpc"
	"doctor-medicine-system/doctorrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type MedicineAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewMedicineAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MedicineAddLogic {
	return &MedicineAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *MedicineAddLogic) MedicineAdd(in *doctorrpc.MedicineAddRequest) (*doctorrpc.MedicineAddResponse, error) {
	// todo: add your logic here and delete this line

	medicine := &mysql.Medicine{
		Model:    gorm.Model{},
		Title:    in.Title,
		Address:  in.Address,
		Desc:     in.Desc,
		Action:   in.Action,
		DoctorId: in.DoctorId,
	}

	if err := medicine.MedicineAdd(mysql.DB); err != nil {
		return nil, err
	}

	return &doctorrpc.MedicineAddResponse{
		MedicineId: int64(medicine.ID),
	}, nil
}
